<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''"
						:highlight-current="true" :expand-on-click-node="false" :default-expand-all="true"
						:filter-node-method="groupFilterNode" @node-click="groupClick">

						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ data.cat_name }}</span>
								<span class="code" v-if="false">{{ node.name }}</span>
								<span class="do">
									<el-button-group>


									</el-button-group>
								</span>
							</span>
						</template>

					</el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div style="display: flex;gap:10px;">
					<div>
						<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					</div>
					<el-button type="primary" plain :disabled="selection.length==0"
						@click="handleSendGift">赠送礼品</el-button>
					<el-input v-model="search.keyword" placeholder="产品名称" clearable style="width: 220px;"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<scTable :params="{selectFilter:selectedValues}" ref="table" :apiObj="apiObj"
					@selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="150"></el-table-column>
					<el-table-column label="头图" width="80">
						<template #default="scope">
							<el-image style="max-width: 35px; max-height: 35px;" preview-teleported hide-on-click-modal
								:src="scope.row.product_image+'?x-oss-process=image/resize,w_35&t='"
								:preview-src-list="[scope.row.product_image]" show-progress fit="contain"
								:z-index="19900501" />
						</template>
					</el-table-column>
					<el-table-column label="产品名称" prop="product_title" width="150">
						<template #default="scope">
							<span @click="table_edit(scope.row, scope.$index)">
								{{scope.row.product_title}}
							</span>
						</template>
					</el-table-column>
					<el-table-column label="产品状态" width="120">
						<template #default="scope">
							<template v-if="scope.row.product_on_sale">
								<template v-if="scope.row.product_off_sale_scheduled_at">
									<el-tag type="success">
										定时下架
									</el-tag>
									<p>{{scope.row.product_off_sale_scheduled_at}}</p>
								</template>
								<el-tag v-else type="success">
									已上架
								</el-tag>
							</template>

							<el-tag type="warning" v-else>
								已下架
							</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="产品销量" align="center" prop="product_sold_count" width="100"
						sortable='custom'></el-table-column>
					<el-table-column label="产品价格" align="center" prop="product_price" width="120">
						<template #default="scope">
							{{scope.row.product_price}} {{scope.row.skus.length>1?'起':''}}
						</template>
					</el-table-column>
					<el-table-column label="所需积分" align="center" prop="product_points" width="100">
						<template #default="scope">
							{{scope.row.product_points}} {{scope.row.skus.length>1?'等':''}}
						</template>
					</el-table-column>
					<el-table-column label="所需活动金" align="center" prop="product_activity_points" width="100"
						v-if="false">
						<template #default="scope">
							{{scope.row.product_activity_points}} {{scope.row.skus.length>1?'等':''}}
						</template>
					</el-table-column>
					<el-table-column v-if="false" label="投放门店" prop="shops" width="200">
						<template #default="scope">
							<el-button-group>
								<template v-for="(item,index) in scope.row.shops" :key="index">
									<el-button text type="primary" size="small">
										{{item.shop_name}}
									</el-button>
								</template>
							</el-button-group>
						</template>
					</el-table-column>
					<el-table-column label="加入时间" prop="created_at" min-width="170" sortable='custom'></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
									@click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button text type="primary" size="small"
									@click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<template v-if="scope.row.product_on_sale">
									<el-popconfirm title="确定要下架吗？" @confirm="table_offline(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">下架</el-button>
										</template>
									</el-popconfirm>
								</template>
								<template v-else>
									<el-popconfirm title="确定要上架吗？" @confirm="table_online(scope.row, scope.$index)">
										<template #reference>
											<el-button text type="primary" size="small">上架</el-button>
										</template>
									</el-popconfirm>
								</template>
							</el-button-group>
						</template>
					</el-table-column>

				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<send-gift v-if="sendGiftDialog.save" ref="sendGiftDialog" @closed="sendGiftDialog.save=false"></send-gift>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'
	import sendGift from './sendGift'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'goods.manage',
		components: {
			saveDialog,
			scSelectFilter,
			sendGift
		},
		data() {
			return {
				selectFilter: [{
						title: "上架状态",
						key: "product_on_sale",
						options: [{
								label: "全部",
								value: null
							},
							{
								label: "已上架",
								value: 1,
								icon: "el-icon-flag"
							},
							{
								label: "未上架",
								value: 0,
								icon: "el-icon-bottom-left"
							}
						]
					},
					{
						title: "审核状态",
						key: "flow_status",
						multiple: false,
						options: [{
								label: "全部",
								value: ""
							},
							{
								label: "待审核",
								value: "20"
							},
							{
								label: "审核中",
								value: "30"
							},
							{
								label: "审核通过",
								value: "40"
							},
							{
								label: "审核驳回",
								value: "50"
							}
						]
					}
				],
				selectedValues: {
					product_on_sale: [1]
				},
				filterData: {},
				dialog: {
					save: false
				},
				sendGiftDialog: {
					save: false
				},
				showGrouploading: false,
				groupFilterText: '',
				group: [],
				apiObj: this.$API.system.goods.list,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			// 礼品赠送
			handleSendGift() {
				if (this.selection.length !== 1) {
					return this.$alert('请选中一条要送的商品', "提示", {
						type: 'error'
					})
				}
				this.sendGiftDialog.save = true
				this.$nextTick(() => {
					this.$refs.sendGiftDialog.open().setData(this.selection[0])
				})
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.saveDialog.open().setData({
						category_id: (checkedNode && checkedNode.id) || null
					});
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			// 上架
			async table_online(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.goods.online.post(reqData);
				if (res.code == 0) {
					row.product_on_sale = true;
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			// 下架
			async table_offline(row, index) {
				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.goods.offline.post(reqData);
				if (res.code == 0) {
					row.product_on_sale = false;
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//加载树数据
			async getGroup() {
				this.showGrouploading = true;
				var res = await this.$API.system.category.tree.get();
				this.showGrouploading = false;
				var allNode = {
					id: '',
					cat_name: '所有',
					dept_path: ''
				}
				res.data.unshift(allNode);
				this.group = res.data;
			},
			//树过滤
			groupFilterNode(value, data) {
				if (!value) return true;
				return data.cat_name.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style>
</style>
